<?php
namespace MyModel;
use MyBase\BaseModel;
class GoodsModel extends BaseModel{

	/**
	 * 添加一条记录
	 */
	public function addGoods($data){
		return $this->add($data);
	}

	/**
	 * 删除记录
	 * $id   可以是数据 可以是单个id
	 */
	public function deleteGoods($id){
		$where = array();
		if(is_numeric($id)){
			$where['goods_id'] = array('EQ',$id);
		}
		if(is_array($id)){
			$where['goods_id'] = array('IN',$id);
		}
		return $this->where($where)->delete();
	}

	/**
	 * 禁用商品
	 * $id   可以是数据 可以是单个id
	 */
	public function forbiddenAndRecoverGoods($id,$status){
		$where = array();
		if(is_numeric($id)){
			$where['goods_id'] = array('EQ',$id);
		}
		if(is_array($id)){
			$where['goods_id'] = array('IN',$id);
		}
		$data['goods_status'] = $status;
		return $this->where($where)->save($data);
	}

	/**
	 * 推荐商品或取消推荐
	 * $id 可以是单个id或是数组
	 */
	public function recommentAndUnRecGoods($id,$recomment_data){
		$where = array();
		if(is_numeric($id)){
			$where['goods_id'] = array('EQ',$id);
		}
		if(is_array($id)){
			$where['goods_id'] = array('IN',$id);
		}
		$data['goods_recomment'] = $recomment_data;
		return $this->where($where)->save($data);		
	}
	/**
	 * 获取记录信息
	 * $id 可以是单个id或是数组
	 */
	public function getGoodsInfoById($id){
		$where = array();
		if(is_numeric($id)){
			$where['goods_id'] = array('EQ',$id);
		}
		if(is_array($id)){
			$where['goods_id'] = array('IN',$id);
		}
		$res = $this->where($where)->select();
		if(count($res) == 0 ){
			return false;
		}
		return $res;
	}
	/**
	 * [updateGoods 更新商品]
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	public function updateGoods($data){
		return $this->save($data);
	}

	/**
	 * 根据条件查询商品
	 */
	public function getGoodsByCondition($where,$columns,$order,$limit=8,$page=1){
		$res = $this->where($where)->field($columns)->order($order)->limit($limit)->page($page)->select();
		// echo $this->getLastSql();
		return $res;
	}


}